package ar.edu.utn.frsf.tpae.a2014.g02.persistence.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import ar.edu.utn.frsf.tpae.a2014.g02.persistence.dao.EmpleadoDao;
import ar.edu.utn.frsf.tpae.a2014.g02.persistence.model.Empleado;

/**
 * DAO de Empleado basado en Hibernate.
 * 
 * @author TPAE
 */
public class EmpleadoHibernateDao implements EmpleadoDao {

	private SessionFactory sessionFactory = new AnnotationConfiguration()
			.configure().buildSessionFactory();


	public Empleado persistir(Empleado usuario) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.saveOrUpdate(usuario);
		session.getTransaction().commit();
		session.close();
		return usuario;
	}

	@SuppressWarnings("unchecked")
	
	public List<Empleado> listar() {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from " + Empleado.class.getName());
		return query.list();
	}

	
	public void deleteAll() {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("delete from " + Empleado.class.getName());
		query.executeUpdate();
	}
}
